{smcl}
{com}{sf}{ul off}{txt}{.-}
      name:  {res}<unnamed>
       {txt}log:  {res}C:\Users\thies\OneDrive\00_Promotion\00_Output\00_Paper\2022_Predicting_Econ_Sanctions\Empirics\20231214_Replication\Log_files/01b_Imposition_full_sample_US.smcl
  {txt}log type:  {res}smcl
 {txt}opened on:  {res}18 Dec 2023, 16:09:39
{txt}
{com}. 
. ***************************************************************
. ***US***
. ***************************************************************
. 
. set seed 1234
{txt}
{com}. 
. *Prepare data
. use "Dataset.dta", clear
{txt}
{com}. keep if sender=="US"
{txt}(10,154 observations deleted)

{com}. 
. * Independent Variables
. gen ln_oil_gas_value_2014 = ln(oil_gas_value_2014+1)
{txt}(681 missing values generated)

{com}. gen sender_colony=US_colony
{txt}(16 missing values generated)

{com}. gen sender_trade = ln_US_Trade_COW
{txt}(448 missing values generated)

{com}. gen coup_dummy = coup1
{txt}(5 missing values generated)

{com}. replace coup_dummy = 0 if coup_dummy == 1
{txt}(61 real changes made)

{com}. replace coup_dummy = 1 if coup_dummy == 2
{txt}(45 real changes made)

{com}. 
. * Dependent variable: 1 if a threat or sanction case was ongoing in the dyad
. gen sanction_threat = sanction_dyad
{txt}
{com}. replace sanction_threat = 1 if threat_dyad==1
{txt}(292 real changes made)

{com}. tab sanction_threat

{txt}sanction_th {c |}
       reat {c |}      Freq.     Percent        Cum.
{hline 12}{c +}{hline 35}
          0 {c |}{res}      3,884       76.50       76.50
{txt}          1 {c |}{res}      1,193       23.50      100.00
{txt}{hline 12}{c +}{hline 35}
      Total {c |}{res}      5,077      100.00
{txt}
{com}. gen sanction_train= sanction_threat if year < 2009
{txt}(1,356 missing values generated)

{com}. gen sanction_test= sanction_threat if year >= 2009
{txt}(3,721 missing values generated)

{com}. 
. * lag time-series variables
. sort ccodecow year
{txt}
{com}. by ccodecow: gen l_v2x_polyarchy = v2x_polyarchy[_n-1] if year==year[_n-1]+1
{txt}(803 missing values generated)

{com}. by ccodecow: gen l_gd_ptss = gd_ptss[_n-1] if year==year[_n-1]+1
{txt}(579 missing values generated)

{com}. by ccodecow: gen l_coup_dummy = coup_dummy[_n-1] if year==year[_n-1]+1
{txt}(203 missing values generated)

{com}. by ccodecow: gen l_one_sided_violence = one_sided_violence[_n-1] if year==year[_n-1]+1
{txt}(199 missing values generated)

{com}. by ccodecow: gen l_conflict = conflict[_n-1] if year==year[_n-1]+1
{txt}(199 missing values generated)

{com}. by ccodecow: gen l_mid_terr_integrity = mid_terr_integrity[_n-1] if year==year[_n-1]+1
{txt}(199 missing values generated)

{com}. by ccodecow: gen l_ln_GDPpc_imputed = ln_GDPpc_imputed[_n-1] if year==year[_n-1]+1
{txt}(318 missing values generated)

{com}. by ccodecow: gen l_sender_trade = sender_trade[_n-1] if year==year[_n-1]+1
{txt}(451 missing values generated)

{com}. by ccodecow: gen l_ln_oil_gas_value = ln_oil_gas_value_2014[_n-1] if year==year[_n-1]+1
{txt}(686 missing values generated)

{com}. by ccodecow: gen l_defense_alliance = defense_alliance[_n-1] if year==year[_n-1]+1
{txt}(199 missing values generated)

{com}. 
. * create dummy variables
. tabulate l_gd_ptss, generate (pol_terr)

  {txt}l_gd_ptss {c |}      Freq.     Percent        Cum.
{hline 12}{c +}{hline 35}
          1 {c |}{res}      1,130       25.12       25.12
{txt}          2 {c |}{res}      1,263       28.08       53.20
{txt}          3 {c |}{res}      1,212       26.95       80.15
{txt}          4 {c |}{res}        635       14.12       94.26
{txt}          5 {c |}{res}        258        5.74      100.00
{txt}{hline 12}{c +}{hline 35}
      Total {c |}{res}      4,498      100.00
{txt}
{com}. 
. * sortieren nach Jahr, zur Vorbereitung RF model
. gen u=0
{txt}
{com}. replace u=1 if year >= 2009
{txt}(1,356 real changes made)

{com}. sort u
{txt}
{com}. 
. ** Imposition
. * Random Forest Model
. rforest sanction_threat l_v2x_polyarchy pol_terr* l_coup_dummy ///
> l_one_sided_violence l_conflict l_mid_terr_integrity ///
> l_ln_GDPpc_imputed l_sender_trade l_ln_oil_gas_value ///
> sender_colony l_defense_alliance in 1/3721, type(class) iter(1500) numvars(15)
{txt}
{com}. 
. * Variable Importance
. ereturn list

{txt}scalars:
       e(Observations) =  {res}3721
           {txt}e(features) =  {res}15
         {txt}e(Iterations) =  {res}1500
          {txt}e(OOB_Error) =  {res}.1238914270357431

{txt}macros:
                e(cmd) : "{res}rforest{txt}"
            e(predict) : "{res}randomforest_predict{txt}"
             e(depvar) : "{res}sanction_threat{txt}"
         e(model_type) : "{res}random forest classification{txt}"

matrices:
         e(importance) : {res} 15 x 1
{txt}
{com}. matrix list e(importance)
{res}
{txt}e(importance)[15,1]
              Variable I~e
l_v2x_poly~y {res}    .75697798
{txt}   pol_terr1 {res}    .30527633
{txt}   pol_terr2 {res}    .46486638
{txt}   pol_terr3 {res}    .84203513
{txt}   pol_terr4 {res}            1
{txt}   pol_terr5 {res}     .8212902
{txt}l_coup_dummy {res}    .22137403
{txt}l_one_side~e {res}    .26801013
{txt}  l_conflict {res}    .18182833
{txt}l_mid_terr~y {res}    .12289162
{txt}l_ln_GDPpc~d {res}    .60468957
{txt}l_sender_t~e {res}    .45717686
{txt}l_ln_oil_g~e {res}    .71343444
{txt}sender_col~y {res}    .02882269
{txt}l_defense_~e {res}    .16596571
{reset}
{com}. * write Variable importance to excel file
. putexcel set "Supplemental_Material\Variable_Importance\Variable_Importance_Imposition_US_RF_full_sample.xlsx", sheet("M") replace
{res}{txt}Note: File will be replaced when the first {cmd:putexcel} command is issued.

{com}. putexcel A1=matrix(e(importance)), names
{res}{txt}file {bf:Supplemental_Material\Variable_Importance\Variable_Importance_Imposition_US_RF_full_sample.xlsx} saved

{com}. 
. * Predictions
. predict randonsUS
{txt}
{com}. predict randonsUS0 randonsUS1, pr
{txt}
{com}. 
. * Confusion Matrix
. * Sensitivity 50.0, Specificity 89.3
. diagtest sanction_test randonsUS

{txt}sanction_t {c |}   predicted classes
       est {c |}         0          1 {c |}     Total
{hline 11}{c +}{hline 22}{c +}{hline 10}
         0 {c |}{res}     1,016        109 {txt}{c |}{res}     1,125 
{txt}         1 {c |}{res}       122        109 {txt}{c |}{res}       231 
{txt}{hline 11}{c +}{hline 22}{c +}{hline 10}
     Total {c |}{res}     1,138        218 {txt}{c |}{res}     1,356 

{txt}True D defined as randonsUS ~= 0                      [95% Conf. Inter.]
-------------------------------------------------------------------------
Sensitivity                     Pr( +| D)  {res}50.00%      47.34%   52.66%
{txt}Specificity                     Pr( -|~D)  {res}89.28%      87.63%   90.93%
{txt}Positive predictive value       Pr( D| +)  {res}47.19%      44.53%   49.84%
{txt}Negative predictive value       Pr(~D| -)  {res}90.31%      88.74%   91.89%
{txt}-------------------------------------------------------------------------
Prevalence                      Pr(D)      {res}16.08%      14.12%   18.03%
{txt}-------------------------------------------------------------------------

{com}. tab2xl sanction_test randonsUS using Supplemental_Material\Prediction_Output\Confusion_Matrixes\US_Imposition_RF_full_sample, row(1) col(1)
{res}{txt}file {bf:Supplemental_Material\Prediction_Output\Confusion_Matrixes\US_Imposition_RF_full_sample.xlsx} saved

{com}. 
. * Kappa .38
. kap sanction_test randonsUS

{txt}{col 14}Expected
Agreement   agreement     Kappa   Std. err.         Z      Prob>Z
{hline 65}
{res}  82.96%      72.37%     0.3835     0.0271      14.13      0.0000
{txt}
{com}. 
. * AUPR .41
. prtab sanction_test randonsUS1
{txt}(1 missing value generated)

{col 12}Number of observations       =  {res}1356
{txt}{col 12}Unique values of classifier  =  {res}1312
{txt}{col 12}Number of positive cases     =  {res}231
{txt}{col 12}Portion of positive cases    ={res}  0.1704

{txt}{hline 50}
{col 5} Recall =  0.1039{col 25}  0.2035{col 37}  0.3030
{hline 50}
{res}{col 2}Precision{col 14}  0.4068{col 25}  0.5000{col 37}  0.5147
{txt}{hline 50}
{res}
{txt}{col 2}Area under precision-recall curve:  0.4130

{com}. graph save "Graph" "Supplemental_Material\Prediction_Output\Roc-curves\AUPR_Imposition_RF_US_full_sample.gph", replace
{txt}{p 0 4 2}
(file {bf}
Supplemental_Material\Prediction_Output\Roc-curves\AUPR_Imposition_RF_US_full_sample.gph{rm}
not found)
{p_end}
{res}{txt}file {bf:Supplemental_Material\Prediction_Output\Roc-curves\AUPR_Imposition_RF_US_full_sample.gph} saved

{com}. 
. log close
      {txt}name:  {res}<unnamed>
       {txt}log:  {res}C:\Users\thies\OneDrive\00_Promotion\00_Output\00_Paper\2022_Predicting_Econ_Sanctions\Empirics\20231214_Replication\Log_files/01b_Imposition_full_sample_US.smcl
  {txt}log type:  {res}smcl
 {txt}closed on:  {res}18 Dec 2023, 16:16:50
{txt}{.-}
{smcl}
{txt}{sf}{ul off}